#include "plotdata.h"

PlotData::PlotData()
{

}

size_t PlotData::size() const
{
    //数据长度
    return m_value.size();
}

QPointF PlotData::sample(size_t i) const
{
    //数据值
    return m_value[i];
}

QRectF PlotData::boundingRect() const
{
    //初始值，默认为0
    return m_boundingRect;
}

void PlotData::setParam(int sampleRate, double xWidth)
{
    m_sampleRate=sampleRate;
    m_xWidth=xWidth;
    m_intervalTime=1000;
    m_intergrationTime=m_intervalTime;
    m_maxCount=m_sampleRate*m_xWidth+1;
    m_listPendingValue.clear();
}

void PlotData::addBaseData(quint64 startTime, QVector<unsigned char> basedata)
{
    int iCount=basedata.count();
    m_value.resize(iCount);
    m_intergrationTime+=startTime;
    for(int i=0;i<iCount;i++)
    {
        //x/y轴
        m_value[i]=QPointF(m_intergrationTime,basedata[i]);
        m_intergrationTime+=m_intervalTime;
    }
}

bool PlotData::updateCurveData(unsigned char recvdata)
{
    QPointF point(0,0);
    point.setX(m_intergrationTime);
    point.setY(recvdata);
    int iCount=m_value.size();
    //判断数据是否能插入
    if(iCount<m_maxCount)
    {
        m_value.append(point);
    }
    else
    {
        //整体偏移
        int idx=0;
        for(idx=0;idx<iCount-1;idx++)
        {
            m_value[idx]=m_value[idx+1];
        }
        m_value[idx]=point;
    }
    m_intergrationTime+=m_intervalTime;
    return true;
}

QwtInterval PlotData::getXInterval()
{
    return  QwtInterval(m_value.first().x(),m_value.last().x());
}
